//
//  HealthyFacialDetailViewController.swift
//  YLH
//
//  Created by qinjiangtao on 2024/1/22.
//

import UIKit
import GKPhotoBrowser

class HealthyFacialDetailViewController: QIBaseViewController {
    
    var model = HealthyFacialModel()
    private var imgBtn = UIButton()
    
    private let reuseIdentifier = "text"
    private var grids = [[Grid]]()
    private let tabulationView = TabulationView()
    /// 行数
    private let rowCount = 4
    /// 列数
    private let colCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "面容健康"
        
        addUI()
        getData()
    }
    
    private func getData() {
        QINetworkManager.requestJson(urlStr: "/Project/faceDetail", method: .get, parameters: ["id": model.id], success: { json in
            let jsonDict = json.dictionaryObject ?? [:]
            print(jsonDict)
            self.generateData(dict: jsonDict)
            self.imgBtn.kf.setImage(with: URL(string: "\(IMAGE_URL)\(json["image"].stringValue)"), for: .normal, placeholder: UIImage(named: "noo"))
        })
        
    }
    
    private func addUI() {
        
        imgBtn = UIButton().then {
            $0.frame = CGRect(x: SCREEN_WIDTH / 2 - 100, y: NAV_HEIGHT, width: 200, height: 200)
            $0.setImage(UIImage(named: "noo"), for: .normal)
            $0.imageView?.contentMode = .scaleAspectFill
            $0.addRoundedCorners(withRadius: 8)
            $0.addTarget(self, action: #selector(imgBtnClick), for: .touchUpInside)
        }
        view.addSubview(imgBtn)
        
        tabulationView.frame = CGRect(x: 10, y:imgBtn.bottom + 20, width: SCREEN_WIDTH - 20, height: CGFloat(40 * rowCount + 5))
        tabulationView.register(TextTabulationViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        tabulationView.dataSource = self
        view.addSubview(tabulationView)
        
    }
    
    @objc private func imgBtnClick() {
        var photos = [GKPhoto]()
        let photo = GKPhoto()
        photo.image = imgBtn.imageView?.image!
        photos.append(photo)
        
        
        let browser = GKPhotoBrowser(photos: photos, currentIndex: 0)
        browser.showStyle = .none
        browser.show(fromVC: self)
        
    }
    
    func generateData(dict: [String: Any]) {
        for row in 0..<rowCount {
            var rowGrids = [Grid]()
            for col in 0..<colCount {
                var text = ""
                var textColor = Color333333
                if col == 0 {
                    if row == 0 {
                        text = "脸龄"
                    } else if row == 1 {
                        text = "左眼形"
                    } else if row == 2 {
                        text = "右眼形"
                    } else if row == 3 {
                        text = "鼻型"
                    }
//                    textColor = UIColor(red: 241.0 / 255.0, green: 171.0 / 255.0, blue: 114.0 / 255.0, alpha: 1)
                    textColor = .orange
                } else if col == 1{
                    if row == 0 {
                        text = "\(dict["face_age"] ?? "")"
                    } else if row == 1 {
                        text = "\(dict["left_eye_name"] ?? "")"
                    } else if row == 2 {
                        text = "\(dict["right_eye_name"] ?? "")"
                    } else if row == 3 {
                        text = "\(dict["nose_name"] ?? "")"
                    }
                } else if col == 2{
                    if row == 0 {
                        text = "脸型"
                    } else if row == 1 {
                        text = "左眉形"
                    } else if row == 2 {
                        text = "右眉形"
                    } else if row == 3 {
                        text = "唇型"
                    }
                    textColor = .orange
                } else if col == 3{
                    if row == 0 {
                        text = "\(dict["face_name"] ?? "")"
                    } else if row == 1 {
                        text = "\(dict["left_eyebrow_name"] ?? "")"
                    } else if row == 2 {
                        text = "\(dict["right_eyebrow_name"] ?? "")"
                    } else if row == 3 {
                        text = "\(dict["mouth_name"] ?? "")"
                    }
                }
                
                let grid = Grid(content: text)
                grid.textColor = textColor
                rowGrids.append(grid)
            }
            grids.append(rowGrids)
        }
        tabulationView.backgroundColor = UIColor(red: 170/255, green: 200/255, blue: 230/255, alpha: 1)
        tabulationView.reloadData()
    }

}


extension HealthyFacialDetailViewController: TabulationViewDataSource {
    
    func numberOfRows(in tabulationView: TabulationView) -> Int {
        return rowCount
    }
    
    func numberOfCols(in tabulationView: TabulationView) -> Int {
        return colCount
    }
    
    //列宽
    func tabulationView(_ tabulationView: TabulationView, lengthForCol col: Int) -> CGFloat {
        return (SCREEN_WIDTH - 20) / 4 - 1
    }
    
    func tabulationView(_ tabulationView: TabulationView, cellForIndexPath indexPath: TabIndexPath) -> TabulationViewCell? {
        
        guard let cell = tabulationView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as? TextTabulationViewCell else {
            return nil
        }
        let grid = grids[indexPath.row][indexPath.col]
        cell.loadGrid(grid)
        return cell
    }
}
